package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：zhao
 * @date ：Created in 2020/1/2 0002
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {
    @Reference
    private PermissionService permissionService;
    /**
     * 新增
     * 1、rpc调用完成新增业务
     * 2、返回结果
     * @param permission
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission){
        log.info("[权限-新增]data:{}",permission);
        permissionService.add(permission);
        return new Result(true,MessageConst.ADD_PERMISSION_SUCCESS);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[权限-分页查询]data:{}", queryPageBean);
        //rpc查询数据
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        //封装返回结果
        return new Result(true,MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.info("[权限-根据id删除]id：{}",id);
        //RPC调用处理业务
        permissionService.delete(id);
        return new Result(true,MessageConst.DELETE_PERMISSION_SUCCESS);
    }

    /**
     * 编辑
     *
     * @param permission
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        log.info("[权限-编辑]data:", permission);
        permissionService.edit(permission);
        return new Result(true, MessageConst.EDIT_PERMISSION_SUCCESS);
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[权限-根据ID查询]id:{}", id);
        Permission permission = permissionService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS,permission);
    }

    /**
     * 查询所有权限
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        log.info("[权限-查询所有]~");
        List<Permission> permissions = permissionService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,permissions);
    }
}

